﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;

namespace AliyunMobileTestPlatform.Models
{
    public class TagsModel
    {
        public Dictionary<string, string> Tags { set; get; }
        public string ProjectName { set; get; }
        public string BranchName { set; get; }

        public TagsModel(string[] TagDirectories,string pName,string bName) 
        {
            ProjectName = pName;
            BranchName = bName;
            Tags = new Dictionary<string, string>();
            foreach (string tagDictionary in TagDirectories)
            {
                string tagPath = tagDictionary.Replace('/', '\\');
                string[] arr = tagPath.Split('\\');
                Tags.Add(arr[arr.Length - 1], tagPath);
            }
        }

        public string ToHtml()
        {
            if (Tags.Count == 0) 
            {
                return "<li class=\"disabled\"><a tabindex=\"-1\" href=\"#\">empty</a></li>";
            }
            StringBuilder sb = new StringBuilder();
            foreach (string tag in Tags.Keys)
            {
                sb.Append("<li>");
                sb.Append(string.Format("<a href=\"#\" role=\"menuitem\" tabindex=\"-1\" onclick=\"onAppListClick('{0}','{1}','{2}')\">",EncodeString(Tags[tag]),ProjectName,BranchName));
                sb.Append(tag);
                sb.Append("</a>");
                sb.Append("</li>");
            }
            return sb.ToString();
        }

        private string EncodeString(string str)
        {
            return System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(str)));
        }
    }
}